home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15278 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.5 KB

  1. Path: hobbes.cc.uga.edu!news
  2. From: Andy Dustman <andy@CCMSD.chem.uga.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template troubles with partial solution (?)
  5. Date: Thu, 04 Apr 1996 11:52:37 -0500
  6. Organization: University of Georgia, Athens
  7. Message-ID: <3163FE55.3E74EA5F@CCMSD.chem.uga.edu>
  8. References: <316354F3.7DEA1F8@CCMSD.chem.uga.edu>
  9. NNTP-Posting-Host: neptune.chem.uga.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; Linux 1.3.80 i586)
  14. CC: andy@ccmsd.chem.uga.edu
  15.  
  16. Andy Dustman wrote:
  17. > I'm trying to modify an existing library of matrix routines into a template
  18. > and I'm having lots of trouble. I'm using G++ 2.7.0, but 2.7.2 seems to have
  19. > the same problems.
  20.  
  21. [deletia]
  22.  
  23. > matrix.h:
  24. > template<class T>
  25. > class matrix {
  26. >         T* mat;
  27. >         ...
  28. >         matrix<T>& operator+= (const matrix<T>& m);
  29. >         ...
  30. > } ;
  31. > template<class T> matrix<T> operator+ (const matrix<T>& m, const matrix<T>& n); \\ A
  32. > *********
  33. > matrix.c:
  34. > #include "matrix.h"
  35. > template<class T> matrix<T> matrix<T>::operator+= (const matrix<T>& m);
  36. > { ... }
  37. > template<class T> matrix<T> operator + (const matrix& m, const matrix& n) \\ B
  38. > {
  39. >         matrix<T> p = m;
  40. >         p += n;
  41. >         return p;
  42. > }
  43. > **********
  44. > dmatrix.c:
  45. > #include "matrix.c"
  46. > template class matrix<double>;
  47. > This compiles, but at link time, I get:
  48. > test.o(.text+0x14d): undefined reference to `operator+(matrix<double> const &, matrix<double> const &)'
  49.  
  50. Somebody sent me some code that is defined essentially the same way. It
  51. compiles and it works. The difference is that his code was all in one file.
  52. So I changed my main.c (not shown) to #include "matrix.c" and not "matrix.h"
  53. so that it would create the template instance matrix<double> within the same
  54. object file. THIS works. But I can't compile a separate object file (like
  55. matrix.c) and get it to link, and as a practical matter, I need to do that.
  56.  
  57. This is with G++ 2.7.0, and I have tried using the -fno-implicit-templates
  58. switch, and that works even worse (even more unresolved symbols). I've also
  59. tried using #pragma directives and -f{alt-}external-templates, with pretty
  60. much the same results. Any suggestions?
  61.  
  62. -- 
  63. Andy Dustman / Computational Center for Molecular Structure and Design / UGA
  64. ===== For PGP public key:  finger andy@neptune.chem.uga.edu | pgp -fka =====
  65. Sure, the Telecomm Act will create jobs: 100,000 new thought-cops on the net
  66. http://charon.chem.uga.edu/~andy    mailto:andy@CCMSD.chem.uga.edu    <}+++<
  67.